'use strict';

angular.module('app')
	.controller('planReportController', [ '$rootScope', '$scope', '$http', '$state',
	                                function($rootScope, $scope, $http, $state) {
		$scope.title = '计划投放报表';
        $scope.param = { };
        $scope.loading = false;
        
		$scope.search = function () {
	        $scope.loading = true;

			$.ajax({
				url : $scope.app.project_name + '/zzyReport/read/list',
				data: $scope.param
			}).then(function(result) {
		        $scope.loading = false;
				if (result.httpCode == 200) {
					$scope.pageInfo = result.data;
				} else {
					$scope.msg = result.msg;
				}
				$scope.$apply();
			});
		}
		
		$scope.searchRe = function () {


	        if(!$("#statsType").val()){
	            $scope.alertModal({'content':'请选择统计类型'});
	            return false;
	        }

	        if ($scope.pageSize == null || $scope.pageSize == "") {
	        	$scope.alertModal({'content':'请选择计划'});
	            return;
	        }

	        if(!$("#startTime").val()){
	            $scope.alertModal({'content':'开始日期不能为空'});
	            return;
	        }

	        if(!$("#endTime").val()){
	            $scope.alertModal({'content':'结束日期不能为空'});
	            return;
	        }

	        if (!checkEndTime($("#startTime").val(), $("#endTime").val())) {
	        	$scope.alertModal({'content':'开始日期不能大于结束日期'});
	    		return;
	    	}

	      $scope.param.planids = $scope.pageSize;

	        $scope.param.startTime = $("#startTime").val();
	        $scope.param.endTime = $("#endTime").val();
            $scope.loading = true;
            $scope.param.pageNum = 1;
	      //
	        $.ajax({
				url : $scope.app.project_name + '/zzyReport/read/list',
				data: $scope.param
			}).then(function(result) {
		        $scope.loading = false;
				if (result.httpCode == 200) {
					$scope.pageInfo = result.data;
				} else {
					$scope.msg = result.msg;
				}
				$scope.$apply();
			});
		}
		
		$scope.getAdvertiserList = function (){
            $.ajax({
                url : $scope.app.project_name + '/userAdverts/read/getAdvertiserList'
            }).then(function(result) {
                $scope.loading = false;
                if (result.httpCode == 200) {
                    $scope.advertiserList = result.data;
                } else {
                    $scope.msg = result.msg;
                }
                $scope.$apply();
            });
		}
		
		$scope.getPlanList = function (){
			var advertiserId = $scope.param.advertiser;
            $.ajax({
                url : $scope.app.project_name + '/userAdverts/read/getPlanList',
                data: {'id': advertiserId}
            }).then(function(result) {
                $scope.loading = false;
                if (result.httpCode == 200) {
                    $scope.pageSizeList = result.data;
                } else {
                    $scope.msg = result.msg;
                }
                $scope.$apply();
            });
		}
		
		/*$scope.pageSizeList=[
            {id:'5',value:'bvcbxcvb'},
            {id:'15',value:'cvbcvbxcvb'},
            {id:'30',value:'xcvbxcvbcxvb'},
        ];*/
		
		$scope.search();
		$scope.getAdvertiserList();
		
		$scope.clearSearch = function() {
			$scope.param = {};
			$scope.param.keyword = null;
			$scope.search();
		}
		
		// 翻页
        $scope.pagination = function (page) {
            $scope.param.pageNum = page;
            $scope.search();
        };
        
        function checkEndTime(startTime, endTime) {  
    	    var start = new Date(startTime.replace("-", "/").replace("-", "/"));  
    	    var end = new Date(endTime.replace("-", "/").replace("-", "/"));  
    	    if (end < start) {  
    	        return false;  
    	    }  
    	    return true;  
    	}

	} ]);